/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.postbar.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author Administrator
 */
public class TransformUtil {

    public static long ipToLong(String ip) {
        if (StringUtils.isBlank(ip)) {
            return 0;
        }
        String[] ipArr = ip.split("\\.");
        if (ipArr.length != 4) {
            return 0;
        }
        long[] iplo = new long[4];
        iplo[0] = Long.parseLong(ipArr[0]);
        iplo[1] = Long.parseLong(ipArr[1]);
        iplo[2] = Long.parseLong(ipArr[2]);
        iplo[3] = Long.parseLong(ipArr[3]);
        return (iplo[0] << 24) + (iplo[1] << 16) + (iplo[2] << 8) + iplo[3];
    }

    public static long timeToLong(String time) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = sdf.parse(time);
        return date.getTime() / 1000;

    }

    public static String sort(String[] strs) {
//        String[] str = new String[]{"头条", "第三条", "头图", "首屏"};
        String[] s = new String[]{"头条", "第几条", "头图", "首屏"};
        StringBuilder result = new StringBuilder();
        for (String item : s) {
            for (String str : strs) {
                int len1 = item.length();
                int len2 = str.length();
                if ((item.substring(0, 1)).equals(str.substring(0, 1)) && (item.substring(len1 - 1, len1)).equals(str.substring(len2 - 1, len2))) {
                    result.append(str);
                    result.append(",");
                    break;
                }
            }
        }
        return result.substring(0, result.length() - 1);
    }

    public static String getWebNameById(String id, HashMap webname) {
        String name = "";
        for (Iterator columnsit = webname.entrySet().iterator(); columnsit.hasNext();) {
            Map.Entry<String, String> entry = (Map.Entry<String, String>) columnsit.next();
            if (entry.getKey().equals(id)) {
                name = entry.getValue();
                break;
            }
        }
        return name;
    }

    public static String getPorvinceIdByUserplace(String id, HashMap provincemap, HashMap placemap) {
        String provinceId = "";
        String name = "";
        for (Iterator columnsit = placemap.entrySet().iterator(); columnsit.hasNext();) {
            Map.Entry<String, String> entry = (Map.Entry<String, String>) columnsit.next();
            if (entry.getKey().equals(id)) {
                name = entry.getValue();
                for (Iterator columnsit2 = provincemap.entrySet().iterator(); columnsit2.hasNext();) {
                    Map.Entry<String, String> provinceEntry = (Map.Entry<String, String>) columnsit2.next();
                    if (entry.getValue().equals(name)) {
                        provinceId = provinceEntry.getKey();
                        break;
                    }
                }
                break;
            }
        }
        return provinceId;
    }

    public static void main(String[] args) throws ParseException {
        String str = "20161205170009";
        long dd = -1;
        try {
            dd = timeToLong(str);
        } catch (ParseException ex) {
            Logger.getLogger(TransformUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println(dd);

        long epoch = new java.text.SimpleDateFormat("yyyyMMddHHmmss").parse("20161205170009").getTime();
        System.out.println(epoch);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        System.out.println(String.valueOf(sdf.parse("20161205170009").getTime() / 1000));

        Long timestamp = dd * 1000;
        String date = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA).format(new Date(timestamp));
        System.out.println(date);

        String j = new java.text.SimpleDateFormat("yyyyMMddHHmmss").format(new java.util.Date(Long.parseLong("1480932355") * 1000));
        System.out.println(j);

        SimpleDateFormat sdfd = new SimpleDateFormat("yyyyMMddHH");
        String currenttime = sdfd.format(new Date());
        System.out.println(currenttime);

        System.out.println(ipToLong("192.168.239.20"));
    }
}
